package com.sincereaddon.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by xjw on 2017/8/25.
 */
public class TimeUtil {

    private static SimpleDateFormat sdf_y = new SimpleDateFormat("yyy-MM-dd");
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat sdf_s = new SimpleDateFormat("HH:mm:ss");

    /**
     * 给定某一格式的字符串 返回 日期
     *
     * @param date yyy-MM-dd 或者  yyy-MM-dd HH:mm:ss
     * @return
     */
    public static Date getDateOfString(String date) {
        Date result = null;
        try {
            if (date.indexOf(":") > -1) {
                result = sdf.parse(date);
            } else {
                result = sdf_y.parse(date);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 给定某一个格式的字符串，返回星期几
     *
     * @param date yyy-MM-dd
     * @return
     */

    public static String getWeekOfString(String date) {
        String result = null;
        try {
            Date date1 = sdf_y.parse(date);
            String[] weekDays = {"日", "一", "二", "三", "四", "五", "六"};
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date1);
            int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
            if (w < 0)
                w = 0;
            result = weekDays[w];
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 返回  yyy-MM-dd
     *
     * @param date
     * @return
     */
    public static String getDate(Date date) {
        return sdf_y.format(date);
    }

    /**
     * 返回  HH:mm:ss
     *
     * @param date
     * @return
     */
    public static String getTime(Date date) {
        return sdf_s.format(date);
    }

    /**
     * 返回  yyy-MM-dd HH:mm:ss
     *
     * @param date
     * @return
     */
    public static String getDateTime(Date date) {
        return sdf.format(date);
    }

    /**
     * utc 时间转为本地时间
     *
     * @param dateTime    utc 时间
     * @param utcFormat
     * @param localFormat
     * @return
     */
    public static String utc2Local(String dateTime, String utcFormat, String localFormat) {

        String reuslt = null;
        try {
            if (utcFormat == null) {
                utcFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'";
            }
            if (localFormat == null) {
                localFormat = "yyyy-MM-dd HH:mm:ss";
            }

            SimpleDateFormat sdf_utc = new SimpleDateFormat(utcFormat);
            SimpleDateFormat sdf_local = new SimpleDateFormat(localFormat);
            sdf_utc.setTimeZone(TimeZone.getTimeZone("UTC"));
            Date date = sdf_utc.parse(dateTime);
            reuslt = sdf_local.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return reuslt;

    }

    /**
     * 本地时间转换为 UTC 时间
     *
     * @param date
     * @param utcFormat
     * @return
     */

    public static String local2Utc(Date date, String utcFormat) {
        String reuslt = null;
        try {
            if (utcFormat == null) {
                utcFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'";
            }
            SimpleDateFormat sdf_utc = new SimpleDateFormat(utcFormat);
            sdf_utc.setTimeZone(TimeZone.getTimeZone("UTC"));
            reuslt = sdf_utc.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return reuslt;

    }

    /**
     * 传入的 n 为 正就是date 的后d天， 为负就是date的前d天
     *
     * @param date
     * @param n
     * @return
     */

    public static String getSpecifiledDay(Date date, int n, String format) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day + n);
        if (format == null) {
            format = "YYYY-MM-DD HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(c.getTime());

    }



}
